package com.bokesoft.yes.mid.cmd;

import com.bokesoft.yes.common.struct.StringHashMap;
import com.bokesoft.yigo.common.util.TypeConvertor;
import com.bokesoft.yigo.meta.factory.IMetaFactory;
import com.bokesoft.yigo.meta.form.MetaFormProfile;
import com.bokesoft.yigo.mid.base.DefaultContext;

/**
 * 重新加载配置，通过将配置清空掉
 */
public class ReloadFormCmd extends DefaultServiceCmd {

	public static final String CMD = "ReloadForm";
	
	private String formKeys = null;
	
	@Override
	public void dealArguments(DefaultContext context,
			StringHashMap<Object> arguments) throws Throwable {
		formKeys = TypeConvertor.toString(arguments.get("formKeys"));
	}

	@Override
	public Object doCmd(DefaultContext context) throws Throwable {
		IMetaFactory metaFactory = context.getVE().getMetaFactory();
		String[] formKeyArray = formKeys.split(",");
		StringBuffer reload = new StringBuffer(128);
		StringBuffer noexist = new StringBuffer(128);
		for (String formKey : formKeyArray) {
			MetaFormProfile metaFormProfile = metaFactory.getMetaFormList().get(formKey);
			if (metaFormProfile != null) {
				//MetaForm metaForm = metaFactory.getMetaForm(formKey);
				//MetaFormNODBProcess.instance.reload(formKey);
				//MetaFormAllFormulScopeCache.instance.clear(metaForm);
				//ScopeTreeBuilder.clear(metaForm);
				metaFactory.reloadMetaForm(formKey);
				//MetaTableCache.reload(formKey);
				if (reload.length() > 0) {
					reload.append(",");
				}
				reload.append(formKey);
			} else {
				if (noexist.length() > 0) {
					noexist.append(",");
				}
				noexist.append(formKey);
			}
		}
		StringBuffer result = new StringBuffer(1024);
		if (reload.length() > 0) {
			result.append(reload).append(" reload. ");
		}
		if (noexist.length() > 0) {
			result.append(noexist).append(" NOT EXIST! ");
		}
		return result.toString();
	}

	@Override
	public IServiceCmd<DefaultContext> newInstance() {
		return new ReloadFormCmd();
	}

	@Override
	public String getCmd() {
		return CMD;
	}
	
}
